package com.until;

import com.dao.IMylog;
import com.model.LOG;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.server.impl.MylogServiceImpl;

@Component
@EnableScheduling
public class AlarmTask {
    private static final SimpleDateFormat myFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private IMylog iMylog;

    LOG myLog = new LOG();

    private int alarmid = 20;
    private String alarmip = "192.161.2.1";
    private int alarmopeweek = 4;
    private String alarmopename = "try";


    /*间隔5秒执行完成一次, cron默认, 定时执行的表示*/
    @Scheduled(fixedRate = 10000)
    public void run1() throws InterruptedException{
        myLog.setIPaddress(alarmip);
        myLog.setOpename(alarmopename);
        myLog.setOpeweek(alarmopeweek);
//        myLog.setUserid(alarmid ++);
        myLog.setOpetime(new Date());
        myLog.setPageCount(0);

        System.out.println("run1 当前时间" + myFormat.format(new Date()));
        iMylog.addMylog(myLog);
        System.out.println("进入数据库插入方法");
        System.out.println(Thread.currentThread().getName()+"======>"+System.currentTimeMillis()/1000);

        Thread.sleep(5000);
        System.out.println("run2 . 当前时间"+myFormat.format(new Date()));

        List<LOG> alarmList = iMylog.getAllMylogs(myLog);
        System.out.println(myLog.toString());
        int tot = alarmList.size();
        System.out.println("数据库中共有数据"+tot+"条");
        System.out.println(Thread.currentThread().getName()+"====>"+System.currentTimeMillis()/1000);

        Thread.sleep(5000);
        System.out.println("run3 . 当前时间"+myFormat.format(new Date()));
        myLog.setPageCount(10);
        List<LOG> alarmList2 = iMylog.getAllMylogs(myLog);
        for (LOG i : alarmList2){
            System.out.println(i.toString());
        }
        System.out.println(Thread.currentThread().getName()+"====>"+System.currentTimeMillis()/1000);

    }

    /*有固定频率, 执行上一个任务开时候, 5秒后再开始执行*/
//    @Scheduled(fixedRate = 5000)
//    public void run1() throws InterruptedException{
//        Thread.sleep(6000);
//        System.out.println("fixedRate . 当前时间"+myFormat.format(new Date()));
//        System.out.println(Thread.currentThread().getName()+"====>"+System.currentTimeMillis()/1000);
//    }

    /*表示固定延迟时间执行, 基于上一个任务完成后延迟5秒, 用的不多*/
//    @Scheduled(fixedDelay = 5000)
//    public void run2() throws InterruptedException{
//        Thread.sleep(5000);
//        System.out.println("run2 . 当前时间"+myFormat.format(new Date()));
//        System.out.println(Thread.currentThread().getName()+"====>"+System.currentTimeMillis()/1000);
//        run3();
//    }
//
//    @Scheduled(fixedDelay = 5000)
//    public void run3() throws InterruptedException{
//        Thread.sleep(10000);
//        System.out.println("run3 . 当前时间"+myFormat.format(new Date()));
//        System.out.println(Thread.currentThread().getName()+"====>"+System.currentTimeMillis()/1000);
//    }

//    @Scheduled(initialDelay = 2000, fixedDelay = 5000)
//    public void run3(){
//        System.out.println("initialDely,当前时间"+myFormat.format(new Date()));
//        System.out.println(Thread.currentThread().getName()+"======>"+System.currentTimeMillis()/1000);
//    }
}
